package com.dd.cloud.user.service.product.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dd.cloud.common.interceptor.UserInterceptor;
import com.dd.cloud.common.utils.Assert;
import com.dd.cloud.common.vo.LoginInfo;
import com.dd.cloud.common.vo.PageReq;
import com.dd.cloud.user.entity.product.ProductBuyRecord;
import com.dd.cloud.user.mapper.product.ProductBuyRecordMapper;
import com.dd.cloud.user.req.product.ProductBuyRecordDoctorPageReq;
import com.dd.cloud.user.req.product.ProductBuyRecordSystemPageReq;
import com.dd.cloud.user.res.product.ProductBuyRecordBarragePageRes;
import com.dd.cloud.user.res.product.ProductBuyRecordDoctorPageRes;
import com.dd.cloud.user.res.product.ProductBuyRecordSystemDetailRes;
import com.dd.cloud.user.res.product.ProductBuyRecordSystemPageRes;
import com.dd.cloud.user.service.product.IProductBuyRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @author Lx
 * @version 1.0
 * @date 2024/1/23
 */
@Slf4j
@Service
public class ProductBuyRecordServiceImpl extends ServiceImpl<ProductBuyRecordMapper, ProductBuyRecord> implements IProductBuyRecordService {
    /**
     * 查询收费服务产品购买记录
     *
     * @param req
     * @return
     */
    @Override
    public IPage<ProductBuyRecordSystemPageRes> queryProductBuyRecordSystemPage(ProductBuyRecordSystemPageReq req) {
        Assert.verifyStatusIdsParam(req.getClass(), req);
        IPage<ProductBuyRecordSystemPageRes> iPage = req.pagePojo();
        iPage = getBaseMapper().queryProductBuyRecordSystemPage(iPage, req);
        return iPage;
    }

    /**
     * 查询收费服务产品购买记录详情
     *
     * @param id
     * @return
     */
    @Override
    public ProductBuyRecordSystemDetailRes queryProductBuyRecordSystemDetail(Integer id) {
        return getBaseMapper().queryProductBuyRecordSystemDetail(id);
    }

    /**
     * 查询服务产品购买记录弹幕
     *
     * @param req
     * @return
     */
    @Override
    public IPage<ProductBuyRecordBarragePageRes> queryProductBuyRecordBarragePage(PageReq req) {
        IPage<ProductBuyRecordBarragePageRes> iPage = req.pagePojo();
        iPage = getBaseMapper().queryProductBuyRecordBarragePage(iPage);
        return iPage;
    }

    /**
     * 医生查询收费服务产品购买记录
     *
     * @param req
     * @return
     */
    @Override
    public IPage<ProductBuyRecordDoctorPageRes> queryProductBuyRecordDoctorPage(ProductBuyRecordDoctorPageReq req) {
        LoginInfo loginInfo = UserInterceptor.getLoginUserInfo(LoginInfo.UserType.DOCTOR).get();
        req.setDoctorId(loginInfo.getId());
        req.setDyDate(LocalDateTime.now().minusMonths(18));
        IPage<ProductBuyRecordDoctorPageRes> iPage = req.pagePojo();
        iPage = getBaseMapper().queryProductBuyRecordDoctorPage(iPage, req);
        return iPage;
    }

    /**
     * 查询当前使用中产品
     *
     * @param doctorId
     * @return
     */
    @Override
    public ProductBuyRecord queryUseProductBuyRecord(Integer doctorId) {
        QueryWrapper<ProductBuyRecord> qw = new QueryWrapper<>();
        qw.lambda().eq(ProductBuyRecord::getStatus, 1)
                .eq(ProductBuyRecord::getDoctorId, doctorId)
                .le(ProductBuyRecord::getStartDate, LocalDateTime.now())
                .ge(ProductBuyRecord::getExpirDate, LocalDateTime.now())
                .orderByAsc(ProductBuyRecord::getStartDate);
        qw.last("limit 1");
        return getOne(qw);
    }
}
